package com.litchishell.jdbcdemo.connection;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @Author: Jiangxx
 * @Date: 2022/04/07
 * @Description:
 */
public class ConnectionUtils {

    private static final String URL = "jdbc:mysql://localhost:3306/jdbc_test";

    private static final String USERNAME = "root";

    private static final String PASSWORD = "root";

    private static final String CLASSNAME = "com.mysql.cj.jdbc.driver";


    public static Connection getConnectionMethod1() {
        Connection connect = null;
        try {
            Driver driver = new com.mysql.cj.jdbc.Driver();

            Properties properties = new Properties();
            properties.put("user", USERNAME);
            properties.put("password", PASSWORD);

            connect = driver.connect(URL, properties);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connect;
    }

    public static Connection getConnectionMethod2() {
        Connection connect = null;
        try {
            Class<?> clazz = Class.forName(CLASSNAME);
            Driver driver = (Driver) clazz.newInstance();

            Properties properties = new Properties();
            properties.put("user", USERNAME);
            properties.put("password", PASSWORD);

            connect = driver.connect(URL, properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connect;
    }

    public static Connection getConnectionMethod3() {
        Connection connect = null;
        try {
            Class<?> clazz = Class.forName(CLASSNAME);
            Driver driver = (Driver) clazz.newInstance();

            DriverManager.registerDriver(driver);

            connect = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connect;
    }

    public static Connection getConnectionMethod4() {
        Connection connect = null;
        try {
            Class.forName(CLASSNAME);
            /**
             * com.mysql.cj.jdbc.Drive类中有
             *     static {
             *         try {
             *             DriverManager.registerDriver(new Driver());
             *         } catch (SQLException var1) {
             *             throw new RuntimeException("Can't register driver!");
             *         }
             *     }
             */
            connect = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connect;
    }

    public static Connection getConnectionMethod5() {
        Connection connect = null;
        try {
            InputStream inStream = ConnectionUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            properties.load(inStream);

            String username = properties.getProperty("user");
            String password = properties.getProperty("password");
            String url = properties.getProperty("url");
            String driverClass = properties.getProperty("driver");

            Class.forName(driverClass);

            connect = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connect;
    }

    public static void closeConnection(Connection connect) {
        if (connect != null) {
            try {
                connect.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
